package proxy;

import google.util.HttpClientFactory;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static google.util.DownLoader.downLoad;

/**
 * Created by zhongwei on 2017/1/13.
 */
public class TestProxy {

    public static void main(String[] args) throws IOException, InterruptedException {
        List<String> list = FileUtils.readLines(new File("peoples"));
//        Document document = Jsoup.connect("https://www.google.com.hk/?gws_rd=ssl#q=%E6%AD%A6%E5%A4%A7&safe=strict&tbm=nws").userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36").validateTLSCertificates(false).timeout(100000000).get();
//        System.out.println(document.text());
//        Pattern pattern = Pattern.compile("找到约(.+?)条结果", Pattern.DOTALL);
//        Matcher matcher = pattern.matcher(document.html());
//        if (matcher.find()) {
//            System.out.println(matcher.group(0));
//        }
        HttpClient client = HttpClientFactory.getNewHttpClient();
        for (String str : list) {
            HttpGet get = new HttpGet("https://www.google.com.hk/?gws_rd=ssl#q=" + URLEncoder.encode(str) + "&safe=strict&tbm=nws");
            Pattern pattern = Pattern.compile("找到约(.+?)条结果", Pattern.DOTALL);
            String content = downLoad(client, get);
            Matcher matcher = pattern.matcher(content);
            if (matcher.find()) {
                System.out.println(matcher.group(0));
            }
        }
    }
}
